﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VWise_DAO;

namespace VWISE_MantVehicles
{
  public partial class RegForm : Form
  {
    CVWise_DAO_Access pVWise_DAO_Access = new CVWise_DAO_Access();
    List<CConductor> lListConductores = null;
    List<CVehicle> lListVehicles = null;
    
    public RegForm()
    {
      InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
      //internal - configuracion de la consulta asincrona
      CLogProcessor.InitLog();
      pVWise_DAO_Access.Configure();

      this.Show();
      botonActualizar.Enabled = false;
        
      FillComboBox();

      botonActualizar.Enabled = true;

      labelStatus.Visible = false;
        
    }

    void FillComboBox()
    {
      lListConductores = pVWise_DAO_Access.GetAllConductores();

      if (lListConductores == null)
      {
        botonActualizar.Enabled = false;
        botonEliminar.Enabled = false;
        return;
      }

      if (lListConductores.Count == 0)
      {
        botonActualizar.Enabled = false;
        botonEliminar.Enabled = false;
        return;
      }


      foreach (CConductor pConductor in lListConductores)
      {
        comboConductor.Items.Add(pConductor.szConductorDesc);
      }

      lListVehicles = pVWise_DAO_Access.GetAllVehicles();

      if (lListVehicles == null)
      {
        botonActualizar.Enabled = false;
        botonEliminar.Enabled = false;
        return;
      }

      if (lListVehicles.Count == 0)
      {
        botonActualizar.Enabled = false;
        botonEliminar.Enabled = false;
        return;
      }


      foreach (CVehicle pVehicle in lListVehicles)
      {
        comboPlaca.Items.Add(pVehicle.szPlaca);
      }

    }

    private void botonActualizar_Click(object sender, EventArgs e)
    {
      DialogResult dResult;
      CVehicle pVehicle;
      int iresult, iSelectedIndex;

      iSelectedIndex = comboPlaca.SelectedIndex;
      if (iSelectedIndex == -1)
      {
        MessageBox.Show(this, "Seleccione algun vehiculo", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        return;
      }

      if (comboConductor.SelectedIndex == -1)
      {
        MessageBox.Show(this, "Seleccione algun conductor", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        return;
      }

      pVehicle = lListVehicles[iSelectedIndex];

      pVehicle.szMarca = textMarca.Text;
      pVehicle.szTipoVehiculo = textTipo.Text;
      pVehicle.szModelo = textModelo.Text;
      pVehicle.szPropietario = textPropietario.Text;
      
      pVehicle.iPKCodeConductor = lListConductores[comboConductor.SelectedIndex].iPKCode;

      dResult = MessageBox.Show(this, "Seguro que desea modificar el vehiculo placa: " + pVehicle.szPlaca, this.Text,
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);

      if (dResult == DialogResult.No)
      {
        return;
      }

      //borramos el vehiculo

      iresult = pVWise_DAO_Access.UpdateVehicle(pVehicle);
      if (iresult != 0)
      {
        //error borrando vehiculo
        MessageBox.Show(this, "No se pudo modificar el vehiculo placa: " + pVehicle.szPlaca, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }

      //borrado de vehiculo ok
      comboPlaca.SelectedIndex = iSelectedIndex;

      MessageBox.Show(this, "Vehiculo placa: " + pVehicle.szPlaca + " fue modificado", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    private void botonCancelar_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void comboPlaca_SelectedIndexChanged(object sender, EventArgs e)
    {
      int iIndex;

      if (comboPlaca.SelectedIndex == -1)
        return;

      CVehicle pVehicle = lListVehicles[comboPlaca.SelectedIndex];
      textCodigo.Text = pVehicle.iPKCode.ToString("000000000");
      textMarca.Text = pVehicle.szMarca;
      textTipo.Text = pVehicle.szTipoVehiculo;
      textModelo.Text = pVehicle.szModelo;
      textPropietario.Text = pVehicle.szPropietario;
      
      iIndex = 0;
      foreach (CConductor pConductor in lListConductores)
      {
        if (pConductor.iPKCode == pVehicle.iPKCodeConductor)
        {
          comboConductor.SelectedIndex = iIndex;
          break;
        }
        iIndex++;
      }
    }

    private void botonEliminar_Click_1(object sender, EventArgs e)
    {
      DialogResult dResult;
      CVehicle pVehicle;
      int iresult,iSelectedIndex;

      iSelectedIndex = comboPlaca.SelectedIndex;
      if (iSelectedIndex == -1)
      {
        MessageBox.Show(this, "Seleccione algun vehiculo", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        return;
      }

      pVehicle = lListVehicles[iSelectedIndex];
      dResult = MessageBox.Show(this, "Seguro que desea eliminar el vehiculo placa: " + pVehicle.szPlaca,this.Text,
                    MessageBoxButtons.YesNo,MessageBoxIcon.Question);

      if (dResult == DialogResult.No)
      {
        return;
      }

      //borramos el vehiculo

      iresult = pVWise_DAO_Access.DeleteVehicle(pVehicle);
      if (iresult != 0)
      {
        //error borrando vehiculo
        MessageBox.Show(this, "No se pudo borrar el vehiculo placa: " + pVehicle.szPlaca, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }

      //borrado de vehiculo ok
      lListVehicles.RemoveAt(iSelectedIndex);
      comboPlaca.Items.RemoveAt(iSelectedIndex);
      comboPlaca.SelectedIndex = 0;

      MessageBox.Show(this, "Vehiculo placa: " + pVehicle.szPlaca + " fue borrado", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
  }
}
